// pages/user-credit/user-credit.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnColor: '#ff614a',
    //签到模块
    sign: "签到",
    signNum: 0, //签到数
    // signState: false, //签到状态
    min: 1, //默认值日期第一天1
    max: 7, //默认值日期最后一天7
    be: 0, //默认倍数
    credit: 0,

    num1: 1,
    num2: 2,
    num3: 3,
    num4: 4,
    num5: 5,
  },
  //签到
  bindSignIn(e) {


    // var credit = this.data.credit
    var min = this.data.min
    var max = this.data.max
    var be = this.data.be
    var signNum = Number(Number(this.data.signNum) + this.data.num1);
    // console.log(credit, signNum, min, max, be)
    var that = this
    if (signNum == 7 || signNum % 7 == 0) {
      var credit = Number(that.data.credit) + that.data.num3
      console.log(credit)
    } else {
      var credit = Number(that.data.credit) + that.data.num1
      console.log(credit)
    }
    if (signNum % 7 == 0) {
      be = Number(be) + that.data.num1
      console.log(be)
    }
    if (Number(signNum) == 7 * Number(be) + 1) {
      min = 7 * Number(be) + that.data.num1
      max = 7 * Number(be) + 7

      var min2 = Number(min) + that.data.num1
      var min3 = Number(min) + that.data.num2
      var min4 = Number(min) + that.data.num3
      var min5 = Number(min) + that.data.num4
      var min6 = Number(min) + that.data.num5

      console.log(min, max)
      that.setData({
        min: min,
        max: max,
        min2: min2,
        min3: min3,
        min4: min4,
        min5: min5,
        min6: min6,
      })
    }
    if (signNum == 1) {
      var timestamp = Date.parse(new Date()) / 1000;
      // console.log('86400000')
      console.log(timestamp)
      wx.request({
        url: 'http://localhost/shopping/users_credit_add.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          username: that.data.name,
          credit: credit,
          min: min,
          max: max,
          be: be,
          signNum: signNum,
          timestamp: timestamp,
        },
        success: function (res) {
          console.log(res.data)
          wx.showToast({
            title: '签到成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else {
      var timestamp = Date.parse(new Date()) / 1000;
      // console.log('86400000')
      console.log(timestamp)
      console.log(Number(timestamp), Number(this.data.timestamp))
      if (Number(Number(timestamp) - Number(this.data.timestamp)) >= 86400) {
        console.log('相隔超过一天了，可以再次签到！')
        wx.request({
          url: 'http://localhost/shopping/users_credit_edit.php',
          header: {
            'Content-Type': 'application/json'
          },
          data: {
            username: that.data.name,
            credit: credit,
            min: min,
            max: max,
            be: be,
            signNum: signNum,
            timestamp: timestamp,
          },
          success: function (res) {
            console.log(res.data)
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 2000,
              signNum: signNum,
              credit: credit,
              min: min,
              max: max,
              be: be,
            })
            that.setData({
              btnColor: '#cc0033',
              sign: "已签到",
            })

          },
          fail: function (res) {
            console.log("fail");
          }
        })
      } else {
        console.log('今天已经签到了！')
        wx.showModal({
          title: '',
          content: '今天已经签到了！',
        })
        that.setData({
          btnColor: '#cc0033',
          sign: "已签到",
        })

      }
      that.setData({

      })
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let name = getApp().globalData.usersname
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/users_credit.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrcredit: res.data.credit,
          min: res.data.credit[0].min,
          max: res.data.credit[0].max,
          signNum: res.data.credit[0].signNum,
          be: res.data.credit[0].be,
          credit: res.data.credit[0].credit,
          timestamp: res.data.credit[0].timestamp,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    var min2 = Number(that.data.min) + that.data.num1
    var min3 = Number(that.data.min) + that.data.num2
    var min4 = Number(that.data.min) + that.data.num3
    var min5 = Number(that.data.min) + that.data.num4
    var min6 = Number(that.data.min) + that.data.num5
    that.setData({
      name: name,
      min2: min2,
      min3: min3,
      min4: min4,
      min5: min5,
      min6: min6,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})